package main2

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"

	"github.com/joho/godotenv"
	//_ "github.com/joho/godotenv/autoload"
)

var (
	client *oss.Client
)

var (
	Endpoint               = ""
	AccessKeyId            = ""
	AccessKeySecret        = ""
	MyBucket               = ""
	UpFile          string = "oss_test.go"
)

func ListBucket() {

	lsRes, err := client.ListBuckets()
	if err != nil {
		panic(err)
	}

	for _, bucket := range lsRes.Buckets {
		fmt.Println("Buckets:", bucket.Name)
	}

}

func UploadFile() {

	bucket, err := client.Bucket(MyBucket)
	if err != nil {
		panic(err)
	}

	fmt.Println(MyBucket)
	fmt.Println(UpFile)
	fmt.Println("111111111111")
	RemoteFile := UpFile
	err = bucket.PutObjectFromFile(RemoteFile, UpFile)
	if err != nil {
		panic(err)
	}
	url, err := bucket.SignURL(UpFile, oss.HTTPGet, 60*60*24)
	if err != nil {
		panic(err)
	}
	fmt.Printf("文件下载url 是%s,  过期时间为1天", url)
}

func TestDownloadFile() {

	bucket, err := client.Bucket(MyBucket)
	if err != nil {
		panic(err)
	}
	err = bucket.GetObjectToFile("2.sql", "3.sql")
	if err != nil {
		panic(err)
	}
}

func init() {
	godotenv.Load("test.env")
	Endpoint = os.Getenv("Endpoint")
	AccessKeyId = os.Getenv("AccessKeyId")
	AccessKeySecret := os.Getenv("AccessKeySecret")
	MyBucket = os.Getenv("MyBucket")
	//s3_MyBucket := os.Getenv("MyBucket")
	//fmt.Println(Endpoint, AccessKeyId, AccessKeySecret)

	c, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
	if err != nil {
		panic(err)
	}
	fmt.Println("初始化成功")
	client = c
}

func main() {
	UploadFile()
}
